﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.ComponentModel;
using MementoClient.Classes;
using MementoLibrary.DTOs;

namespace MementoClient.ViewModels
{
    public class WorkLogVM : INotifyPropertyChanged
    {
        #region Private Member Variables
        private decimal _minutes;
        private TaskDTO _task;
        private int _workLogId;
        private DateTime _workdate;
        private CategoryDTO selectedCategory;
        private CategoryGroupDTO selectedCategoryGroup;
        #endregion

        #region Constructors
        public WorkLogVM()
        {
            Categories = new ObservableCollection<CategoryDTO>();
            AddThisCategory = new RelayCommand(AddThisCategoryExecute, o => SelectedCategory != null);
        }
        #endregion

        #region Public Properties
        public RelayCommand AddThisCategory { get; set; }
        public RelayCommand Submit { get; set; }
        public int WorkLogId
        {
            get { return _workLogId; }
            set
            {
                _workLogId = value;
                PropertyChange("WorkLogId");
            }
        }
        public decimal Minutes
        {
            get { return _minutes; }
            set
            {
                _minutes = value;
                PropertyChange("Minutes");
                PropertyChange("DisplayString");
            }
        }
        public DateTime Workdate
        {
            get { return _workdate; }
            set
            {
                _workdate = value;
                PropertyChange("Workdate");
                PropertyChange("DisplayString");
            }
        }
        public TaskDTO Task
        {
            get { return _task; }
            set
            {
                _task = value;
                PropertyChange("Task");
            }
        }
        public ObservableCollection<CategoryDTO> Categories { get; set; }
        public IEnumerable<CategoryGroupDTO> AvailableCategoryGroups
        {
            get
            {
                return CategoryGroups.Where(cg => cg.Categories.Any(a => Categories.Contains(a)) == false);
            }
        }
        public IEnumerable<CategoryGroupDTO> CategoryGroups { get; set; }
        public bool AreCategoryGroupsAvailable
        {
            get { return AvailableCategoryGroups != null && AvailableCategoryGroups.Any(); }
        }
        public bool AreCategoryGroupsNotAvailable
        {
            get { return AvailableCategoryGroups == null || AvailableCategoryGroups.Any() == false; }
        }
        public CategoryGroupDTO SelectedCategoryGroup
        {
            get { return selectedCategoryGroup; }
            set
            {
                selectedCategoryGroup = value;
                PropertyChange("SelectedCategoryGroup");
            }
        }
        public CategoryDTO SelectedCategory
        {
            get { return selectedCategory; }
            set
            {
                selectedCategory = value;
                PropertyChange("SelectedCategory");
            }
        }
        public string DisplayString
        {
            get { return string.Format("{0} minutes on {1}", Minutes, Workdate.ToShortDateString()); }
        }
        public void AddThisCategoryExecute(object o)
        {
            Categories.Add(SelectedCategory);
            PropertyChange("AvailableCategoryGroups");
            PropertyChange("AreCategoryGroupsAvailable");
            PropertyChange("AreCategoryGroupsNotAvailable");
        }
        public override string ToString()
        {
            return DisplayString;
        }
        public void PropertyChange(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
